
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.Scanner;

/**
 * @version 1.0
 * @program: 集合demo
 * @description:
 * @author: Kishan Liu
 * @create: 2022-01-19 10:47
 **/
public class Serve {
    static Collection<Object> shoppingTrolley = new ArrayList<>();
    static int price = 0;
    static boolean judge = true;
    public static void main(String[] args) {
        do {
            mainMenu();
        }while (judge);


    }


    public static void mainMenu(){
        int select;
        Scanner in;
        for (;;){
            System.out.println("1.查看商品信息");
            System.out.println("2.添加商品至购物车");
            System.out.println("3.删除商品");
            System.out.println("4.查看并结算购物车");
            System.out.println("5.退出本程序");
            in = new Scanner(System.in);
            select = in.nextInt();
            if (select > 0 && select < 6){
                break;
            }
        }
        switch (select){
            case 2:
                //添加商品
                switch (selectMenu()) {
                    case 1 -> {shoppingTrolley.add(new Product("Cola"));price+=3;System.out.println("添加成功！");}
                    case 2 -> {shoppingTrolley.add(new Product("Sprite"));price+=3;System.out.println("添加成功！");}
                    case 3 -> {shoppingTrolley.add(new Product("juice"));price+=4;System.out.println("添加成功！");}
                    case 4 -> {shoppingTrolley.add(new Product("iceBlackTea"));price+=3;System.out.println("添加成功！");}
                    case 5 -> {shoppingTrolley.add(new Product("slicedBread"));price+=30;System.out.println("添加成功！");}
                    case 6 -> {shoppingTrolley.add(new Product("panpanRolls"));price+=31;System.out.println("添加成功！");}
                }
                break;
            case 1:
                //查看商品信息
                switch (selectMenu()) {
                    case 1 -> {
                        Product Cola = new Product("Cola");
                        System.out.println(Cola);
                    }
                    case 2 -> {
                        Product Sprite = new Product("Sprite");
                        System.out.println(Sprite);
                    }
                    case 3 -> {
                        Product juice = new Product("juice");
                        System.out.println(juice);
                    }
                    case 4 -> {
                        Product iceBlackTea = new Product("iceBlackTea");
                        System.out.println(iceBlackTea);
                    }
                    case 5 -> {
                        Product slicedBread = new Product("slicedBread");
                        System.out.println(slicedBread);
                    }
                    case 6 -> {
                        Product panpanRolls = new Product("panpanRolls");
                        System.out.println(panpanRolls);
                    }
                }
                break;
            case 3:
                //删除信息
                int n = selectMenu();
                if (n == 1 ){
                    if (shoppingTrolley.contains(new Product("Cola"))) {
                    shoppingTrolley.remove(new Product("Cola"));
                    price -= 3;
                        System.out.println("删除成功！");
                    }
                    else {
                    System.out.println("购物车中无该商品！");
                    }
                break;
            }if (n == 2 ){
                    if (shoppingTrolley.contains(new Product("Sprite"))) {
                    shoppingTrolley.remove(new Product("Sprite"));
                    price -= 3;
                    System.out.println("删除成功！");
                    }
                    else {
                    System.out.println("购物车中无该商品！");
                    }
                break;
            }if (n == 3 ){
                    if (shoppingTrolley.contains(new Product("juice"))) {
                    shoppingTrolley.remove(new Product("juice"));
                    price -= 4;
                    System.out.println("删除成功！");
                    }
                    else {
                    System.out.println("购物车中无该商品！");
                    }
                break;
            }if (n == 4 ){
                    if (shoppingTrolley.contains(new Product("iceBlackTea"))) {
                    shoppingTrolley.remove(new Product("iceBlackTea"));
                    price -= 3;
                    System.out.println("删除成功！");
                    }
                    else {
                    System.out.println("购物车中无该商品！");
                    }
                break;
            }if (n == 5 ){
                    if (shoppingTrolley.contains(new Product("slicedBread"))) {
                    shoppingTrolley.remove(new Product("slicedBread"));
                    price -= 30;
                    System.out.println("删除成功！");
                    }
                    else {
                    System.out.println("购物车中无该商品！");
                    }
                break;
            }if (n == 6 ){
                    if (shoppingTrolley.contains(new Product("panpanRolls"))) {
                    shoppingTrolley.remove(new Product("panpanRolls"));
                    price -= 31;
                    System.out.println("删除成功！");
                    }
                    else {
                    System.out.println("购物车中无该商品！");
                    }
                break;
            }


            case 4:
                //查看购物车
                System.out.println("购物车中的商品有：");
                System.out.println(shoppingTrolley);
                System.out.println("总计花费"+price+"元");
                Scanner scanner;
                for(;;) {
                    scanner = new Scanner(System.in);
                    System.out.println("是否结算(y/n)");
                    if (Objects.equals(scanner.next(), "n") || Objects.equals(scanner.next(), "N"))
                        break;
                    else if (Objects.equals(scanner.next(), "y") || Objects.equals(scanner.next(), "Y")){
                        judge = false;
                        break;
                    }
                }break;
            case 5:
                judge = false;
        }

    }

    //商品选择导航
    public static int selectMenu() {
        int select;
        Scanner in;
        for (;;){
            System.out.println("请选择商品种类：");
            System.out.println("1.饮料");
            System.out.println("2.面包");
            in = new Scanner(System.in);
            select = in.nextInt();
            if (select > 0 && select < 3){
                break;
            }else System.out.println("输入错误，请重新输入！");
        }
        if (select==1){
            for (;;){
                System.out.println("请选择目标商品：");
                System.out.println("1.可口可乐");
                System.out.println("2.雪碧");
                System.out.println("3.果粒橙");
                System.out.println("4.冰红茶");
                System.out.println("（请输入商品前的编码）");
                in = new Scanner(System.in);
                select = in.nextInt();
                if (select > 0 && select < 5){
                    return select;
                }else System.out.println("输入错误，请重新输入！");
            }
        }else {
            for (;;){
                System.out.println("请选择您的想要选购的商品：");
                System.out.println("1.切片面包");
                System.out.println("2.法式盼盼小面包");
                System.out.println("（请输入商品前的编码）");
                in = new Scanner(System.in);
                select = in.nextInt();
                if (select > 0 && select < 3){
                    return select+4;
                }else System.out.println("输入错误，请重新输入！");

            }

        }

    }
}


